﻿using UnityEngine;
using System.Collections;
using System.Reflection;


public static class FieldInfoExtension
{
    public static string GetTypeRef(this FieldInfo info)
    {
        object[] attributes = CustomAttributeCache.GetCustomAttributes(info);
        foreach (object attribute in attributes)
        {
            if (attribute is FieldInfoAttribute)
            {
                return (attribute as FieldInfoAttribute).typeRef;
            }
        }
        return string.Empty;
    }

    public static string GetFieldName(this FieldInfo info)
    {
        object[] attributes = CustomAttributeCache.GetCustomAttributes(info);
        foreach (object attribute in attributes)
        {
            if (attribute is FieldInfoAttribute)
            {
                return (attribute as FieldInfoAttribute).fieldName;
            }
        }
        return info.Name;
    }

    public static string GetDirty(this FieldInfo info)
    {
        object[] attributes = CustomAttributeCache.GetCustomAttributes(info);
        foreach (object attribute in attributes)
        {
            if (attribute is FieldInfoAttribute)
            {
                return (attribute as FieldInfoAttribute).dirtyField;
            }
        }
        return string.Empty;
    }



    public static string GetToolTip(this FieldInfo info)
    {
        object[] attributes = CustomAttributeCache.GetCustomAttributes(info);
        for (int i = 0; i < attributes.Length; ++i)
        {
            if (attributes[i] is FieldInfoAttribute)
            {
                return (attributes[i] as FieldInfoAttribute).tooltip;
            }
        }
        return string.Empty;
    }

    public static object GetDefaultValue(this FieldInfo info)
    {
        object[] attributes = CustomAttributeCache.GetCustomAttributes(info);
        foreach (object attribute in attributes)
        {
            if (attribute is FieldInfoAttribute)
            {
                return (attribute as FieldInfoAttribute).defaultValue;
            }
        }
        return null;
    }

    public static string GetNullLabel(this FieldInfo info)
    {
        object[] attributes = CustomAttributeCache.GetCustomAttributes(info);
        foreach (object attribute in attributes)
        {
            if (attribute is FieldInfoAttribute)
            {
                return (attribute as FieldInfoAttribute).nullLabel;
            }
        }
        return string.Empty;
    }


    public static bool IsFieldRequired(this FieldInfo info)
    {
        object[] attributes = CustomAttributeCache.GetCustomAttributes(info);
        foreach (object attribute in attributes)
        {
            if (attribute is FieldInfoAttribute)
            {
                return (attribute as FieldInfoAttribute).requiredField;
            }
        }
        return false;
    }

  
}
